<template>
  <a-modal
    title="抄表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-row :gutter="24">
        <a-col :md="12" :sm="24">
          <a-form :form="form">
            <a-form-item v-show="false">
              <a-input  v-decorator="['id']" />
            </a-form-item>
            <a-form-item v-show="false">
              <a-input  v-decorator="['houseBillId']" />
            </a-form-item>
            <a-form-item
              label="抄表日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
            <a-date-picker style="width: 100%" placeholder="请选择抄表日期" v-decorator="['meterReadingDate', { initialValue: moment().format('YYYY-MM-DD'), rules: [{ required: true, message: '请选择抄表日期！' }]}]" @change="onChangemeterReadingDate"/>
            </a-form-item>
          </a-form>
        </a-col>
        <a-col :md="12" :sm="24" >
          <a-form :form="form">
            <a-form-item
              label="收租日"
              :disabled="true"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
            <a-date-picker style="width: 100%" placeholder="请选择收租日" v-decorator="['rentedDay']"/>
            </a-form-item>
          </a-form>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :md="24" :sm="24">
          <a-table
            size="middle"
            :columns="columns"
            :dataSource="dataMeter"
            :pagination="false"
            :loading="memberLoading">
            <template slot="reading" slot-scope="text, record">
              <a-input-number
                style="width:100%"
                :precision="2"
                :min="0"
                :value="text"
                :max="99999999"
                @change="e => handleChange(e,record.id, 'reading')"
                :step="10"/>
              </template>
            </a-table>
        </a-col>
      </a-row>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { hydropowerMeterEdit, SdFyListByDetail } from '@/api/modular/main/hydropowermeter/hydropowerMeterManage'
  export default {
    data () {
      return {
        id: '',
        houseLeaseId: '',
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        memberLoading: false,
        form: this.$form.createForm(this),
        columns: [
          {
            title: '表類型',
            dataIndex: 'typeName',
            width: '20%',
            scopedSlots: { customRender: 'typeName' }
          },
          {
            title: '上期讀數',
            dataIndex: 'preReading',
            width: '20%',
            scopedSlots: { customRender: 'preReading' }
          },
          {
            title: '本次讀數',
            dataIndex: 'reading',
            width: '20%',
            scopedSlots: { customRender: 'reading' }
          },
          {
            title: '用量',
            dataIndex: 'dosage',
            width: '20%',
            scopedSlots: { customRender: 'dosage' }
          },
          {
            title: '費用',
            dataIndex: 'cost',
            width: '20%',
            scopedSlots: { customRender: 'cost' }
          }
        ],
        dataMeter: [],
        meterReadingDateDateString: moment().format('YYYY-MM-DD')
      }
    },
    methods: {
      moment,
      // 初始化方法
      add (record) {
        this.visible = true
        this.id = record.id
        this.houseLeaseId = record.houseLeaseId
        this.getSdFyListByDetail()
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              houseLeaseId: record.houseLeaseId,
              rentedDay: record.rentedDay
            }
          )
        }, 100)
      },
      getSdFyListByDetail() {
        SdFyListByDetail({ houseBillId: this.id, houseLeaseId: this.houseLeaseId }).then((res) => {
              if (res.code === 200) {
                this.dataMeter = res.data
                this.confirmLoading = false
              } else {
                this.$message.error('查詢失敗')
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
      },
      /**
       * 选择子表单单项触发
       */
      handleChange (value, key, column) {
        const newData = [...this.dataMeter]
        const target = newData.find(item => key === item.id)
        if (target) {
          target[column] = value
          const preReading = target['preReading']
          if (value < preReading) {
            target[column] = preReading
          }
          if (target[column] - preReading < target['solder']) {
            target['dosage'] = target['solder']
          } else {
            target['dosage'] = target[column] - preReading
          }
          target['cost'] = target['dosage'] * target['amountDu']
          this.dataMeter = newData
        }
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            debugger
            const strDate = this.meterReadingDateDateString
            hydropowerMeterEdit({ meterReadingDate: strDate, data: this.dataMeter }).then((res) => {
              if (res.code === 200) {
                this.$message.success('抄表成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('抄表失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangemeterReadingDate(date, dateString) {
        this.meterReadingDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
        this.id = ''
        this.houseLeaseId = ''
      }
    }
  }
</script>
